home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12000 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: news1.erols.com!newsmaster@erols.com
  2. From: Chris Cobb <ccobb@erols.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need help on static data members of class templates
  5. Date: 17 Mar 1996 17:45:54 GMT
  6. Organization: CSEG, Inc.
  7. Message-ID: <4ihj4i$2ni@news5.erols.com>
  8. References: <4ig6pf$aqp@agate.berkeley.edu>
  9. NNTP-Posting-Host: ccobb.erols.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22KIT (Windows; U; 16bit)
  14.  
  15. parsons@vouvray.CS.Berkeley.EDU (David C. Parsons) wrote:
  16. >I've been having a lot of difficulty getting static data members of
  17. >class templates to work right.  Specifically, things get very weird
  18. >when I try to instantiate the template with the type parameter equal
  19. >to another instantiation of the template. 
  20.  
  21. I tried your code under Borland C++.  It compiles and produces the
  22. following output:
  23. instantiating C: classVar is 3
  24. instantiating C: classVar is 3
  25. instantiating C: classVar is [object of class C having classVar 3]
  26.  
  27. Even with full warnings on, it only complains about main not returning
  28. a value.
  29.  
  30. What version of gcc are you using and what platform?
  31.  
  32. Chris
  33. ccobb@cseg.com
  34.  
  35. >
  36. >template< class T >
  37. >class C
  38. >{
  39. >public:
  40. >
  41. >    static const T classVar;
  42. >    C()
  43. >    {
  44. >    cout << "instantiating C: classVar is " << classVar << endl;
  45. >    i = 100;  // this is line 12
  46. >    }
  47. >    int i;
  48. >};  // end of class C
  49. >
  50. >template< class T >
  51. >ostream& operator <<( ostream& os, const C< T >& c )
  52. >{
  53. >    os << "[object of class C having classVar " << c.classVar << "]";
  54. >    return os;
  55. >}
  56. >
  57. >// initialize static members of template instantiations we'll need
  58. >
  59. >const int C<int>::classVar = 3;
  60. >const C< int > C< C<int> >::classVar = C<int>();  // this is line 27
  61. >
  62. >int main()
  63. >{
  64. >    C< int > c1;
  65. >    C< C<int> > c2;
  66. >}
  67. >
  68. >//-- end code sample --------------------------------
  69. >
  70. >This compiles fine under g++ (with only a warning about unused
  71. >variables c1 and c2), but when run it produces:
  72. >
  73. >instantiating C: classVar is 3
  74. >Bus error (core dumped)
  75. >
  76. >From gdb, it appears that the error is occurring during the
  77. >construction of the C< C<int> >::classVar object:
  78. >
  79. >#0  0x4590 in C<int>::C (this=0x36b0) at scratch.C:12
  80. >#1  0x4478 in global constructors keyed to
  81. >           C<int>::classVar () at scratch.C:27
  82. >#2  0x1d00c in __do_global_ctors ()
  83. >#3  0x1d050 in __main ()
  84. >#4  0x43a0 in main () at scratch.C:30
  85. >
  86. >Surprisingly (to me at least), the whole problem goes away if I
  87. >comment out the assignment i = 100; in the constructor (line 12
  88. >above).  The output is then:
  89. >
  90. >instantiating C: classVar is 3
  91. >instantiating C: classVar is 3
  92. >instantiating C: classVar is [object of class C having classVar 3]
  93. >
  94. >In summary, it seems that assigning to any data member in the
  95. >constructor makes it impossible to instantiate the template with
  96. >another instance of the same template.  I do not understand this
  97. >restriction. 
  98. >
  99. >I've tried every variation of this code I can think of, but nothing
  100. >seems to work.  Anyone have any ideas?  I'd really like to get this to
  101. >work, as it forms part of an otherwise very elegant sparse matrix
  102. >package.
  103. >
  104. >David Parsons
  105.  
  106.  
  107.